﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Xml;
using System.IO;

namespace FileChat
{
    public partial class ChatForm : Form
    {
        public User User =new User();
        public User CurrentUser = new User();
        public String FileChatBasePath;
        public delegate void MessageComing(FileChatMessage msg);

        public ChatForm()
        {
            InitializeComponent();
        }

        public void ShowMessage(FileChatMessage msg)
        {
            MessageComing messageComing = new MessageComing(AddNewMessage);
            this.Invoke(messageComing, new object[] { msg });
        }

        private void ChatForm_Load(object sender, EventArgs e)
        {
            try {
                foreach (PropertyInfo pi in User.GetType().GetProperties())
                {
                    Control[] controls =  this.panel1.Controls.Find(string.Format("lbl{0}", pi.Name), false);
                    if (controls.Length != 0)
                    {
                        Label lbl = controls[0] as Label;
                        lbl.Text = pi.GetValue(User, null).ToString();
                    }
                }
            }catch(Exception ex){
                MessageBox.Show(ex.Message);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try {
                //向目标用户的文件夹里写一个xml聊天信息 或二进制聊天信息流
                string targetFilePath = string.Format(String.Format("{0}/ChatData/{1}/rcvd", FileChatBasePath, User.ID));
                string msgPath = string.Format("{0}/{1}.xml", targetFilePath, string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now));
                if (!Directory.Exists(targetFilePath))
                {
                    Directory.CreateDirectory(targetFilePath);
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(Properties.Resources.Message_xml);

                //写入聊天信息至xml   写入发消息人
                XmlNode root = xmlDoc.SelectSingleNode("FileChat/Message");
                XmlElement userXml = xmlDoc.CreateElement("User");

                foreach (PropertyInfo pi in CurrentUser.GetType().GetProperties())
                {
                    if (pi.Name != "Password") {
                        userXml.SetAttribute(pi.Name, pi.GetValue(CurrentUser, null).ToString());
                    }          
                }
                root.AppendChild(userXml);

                //写入时间 
                XmlElement timeXml = xmlDoc.CreateElement("Time");
                timeXml.InnerText = DateTime.Now.ToString();
                root.AppendChild(timeXml);
                //写入消息内容
                XmlElement msgXml = xmlDoc.CreateElement("Content");
               
                XmlCDataSection msg = xmlDoc.CreateCDataSection("Content");
                msg.Value = txtSend.Rtf;
                richTextBox1.SelectionStart = richTextBox1.Text.Length;
                richTextBox1.SelectionColor = Color.Green;
                richTextBox1.SelectedText = string.Format("\n{0}  {1}\n", CurrentUser.DisplayName, DateTime.Now.ToString());
                richTextBox1.SelectionStart = richTextBox1.Text.Length;
                richTextBox1.SelectedRtf = txtSend.Rtf;
                richTextBox1.SelectionStart = richTextBox1.Text.Length;
                richTextBox1.Focus();
                txtSend.Text = "";
                msgXml.AppendChild(msg);
                root.AppendChild(msgXml);
                xmlDoc.Save(msgPath);
            }catch(Exception ex){
                MessageBox.Show(ex.Message);
            
            }
        }

        public void AddNewMessage(FileChatMessage message) {
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.SelectionColor = Color.Green;
            richTextBox1.SelectedText = string.Format("\n{0}  {1}\n", message.Sender.DisplayName, message.Time);
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.SelectedRtf = message.Content;
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.Focus();
        }

        private void ChatForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 90) { 
                
            }
        }
    }
}
